<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\User;
use app\admin\model\UserAddress;
use app\admin\model\Prize;
use app\admin\model\PrizeUser;
use app\admin\model\UserReport;
use app\admin\model\ProductReport;
use app\admin\model\UserFraze;
use app\admin\model\Category;
use app\admin\model\ProductTake;
use app\admin\model\Follow;
use app\admin\model\UserCollect;
use app\admin\model\ParticipationProduct;
use app\admin\model\Product as ProductModel;
use app\admin\model\ProductWish;
use app\admin\model\ProductMessage;
use app\admin\model\ConfigData;
use app\admin\model\Article;
use app\admin\model\ProductViewLog;
use app\admin\model\UserRoseLog;
use app\admin\model\UserOrder;
use app\admin\model\Slideshow;
use app\admin\model\SArea;
use app\admin\model\ProductCategory;
use app\admin\model\Community;
use GuzzleHttp\Handler\Proxy;

/**
 * 产品控制器
 */
class Product extends Api
{
	protected $noNeedLogin = ['getLoveList','isprize','want_count_add','products','products_wish','product_related','product_wish_detail','product_message','product_detail','getuserproduct'];

	public function _initialize(){
		$action = request()->action();
		$ignore = array('host','accept','content-length','content-type');
		$headers = array();
		if($this->request->request('token')){
			$headers['token'] = $this->request->request('token');
		}else{
			foreach($_SERVER as $key=>$value){
				if(substr($key, 0, 5)==='HTTP_'){
					$key = substr($key, 5);
					$key = str_replace('_', ' ', $key);
					$key = str_replace(' ', '-', $key);
					$key = strtolower($key);
					if(!in_array($key, $ignore)){
						$headers[$key] = $value;
					}
				}
			}
		}
		if(empty($headers['token']) && in_array($action,$this->noNeedLogin)){
			//当未传token并且控制器可不不传token情况下不需要验证登录
		}else{
			$token=$headers['token'];
			if(strlen($token)!=32){
				$this->error('请登录');
			}
			$this->user=User::where(array('token'=>$token))->find();
			if(!$this->user){
				$this->error('请登录');
			}
			if($this->user['status'] != 1){
                $this->error('账号异常');
            }
			User::where(array('id'=>$this->user['id']))->update(array('active_time'=>time()));
		}
	
		
		
	}
	//发布闲置
    public function product_save_new(){
		$id			            = $this->request->request('product_id','');
		$data['category_id']	= $this->request->request('category_id','');
		$data['story']			= $this->request->request('story','');
		$data['title']			= $this->request->request('title','');
		$data['peisong']        = $this->request->request('peisong','');
		$data['video_img']		= $this->request->request('video_img','');
		$data['postage']		= $this->request->request('postage','');
		$data['address']		= $this->request->request('address','');
		$data['province']		= $this->request->request('province','');
		$data['city']			= $this->request->request('city','');
		$data['area']			= $this->request->request('area','');
		$data['price']			= $this->request->request('price',0);
		$data['shipping_fee']	= $this->request->request('shipping_fee',0);
		$data['longitude']		= $this->request->request('longitude',0);
		$data['latitude']		= $this->request->request('latitude',0);
		$data['imgs']			= $this->request->request('imgs','');
		$data['name']           = $this->request->request('name');
		$data['phone']          = $this->request->request('phone','');
		$data['weight']         = $this->request->request('weight','1kg');
		$data['status']         = $this->request->request('status',0);
		$data['wx']         = $this->request->request('wx','');
		$data['qq']         = $this->request->request('qq','');
		$data['xiaoqu']         = $this->request->request('xiaoqu','');
		$data['xq_latitude']         = $this->request->request('xq_latitude','');
		$data['xq_longitude']         = $this->request->request('xq_longitude','');
		$data['type_status']    = $this->request->request('type_status',1);
		$line_1	=	$this->request->request('line_1','');
		$line_2	=	$this->request->request('line_2','');
		$town	=	$this->request->request('town','');
		$county	=	$this->request->request('county','');
		$postcode	=	$this->request->request('postcode','');
		$zhi	=	$this->request->request('zhi','');
		$is_prize         = $this->request->request('is_prize','');
		if(strpos($data['weight'], "kg") === false){
			$data['weight'] = $data['weight'].'kg';
		}
		if($data['peisong'] == '0,1' && $data['weight'] == 'kg'){
			$data['weight'] = '1kg';
		}
		if($data['xiaoqu'] == '某某小区'){
			$data['xiaoqu'] = '';	
		}
		if(mb_strlen($data['title'], 'UTF-8') < 6){
			$this->error('物品需要清晰描述，请输入6个字以上');
		}
		$data['video_img'] = $data['video_img'] == 'undefined' ? '' : $data['video_img'];
        $data['imgs'] = $data['imgs'] == 'undefined' ? '' : $data['imgs'];
		if(!$data['longitude'] || !$data['latitude']){
			$fid = SArea::where(['name'=>['like','%'.$data['province'].'%']])->value('id');
			if($data['city'] != '市辖区' && $data['province'] != '重庆' && $data['province'] != '上海' && $data['province'] != '天津' && $data['province'] != '北京市' && $data['area']){
				$fid = SArea::where(['name'=>['like','%'.$data['city'].'%'],'fid'=>$fid])->value('id');
			}
			if(!$data['area']){
				$info = SArea::where(['name'=>['like','%'.$data['city'].'%'],'fid'=>$fid])->find();
			}else{
				$info = SArea::where(['name'=>['like','%'.$data['area'].'%'],'fid'=>$fid])->find();
			}
			$data['longitude'] = $info['lng'];
			$data['latitude'] = $info['lat'];
			
		}
		if(!$data['longitude'] || !$data['latitude']){
			$data['longitude'] = '';
			$data['latitude'] = '';
			$this->error('地址错误请更换地址后重新发布');
		}
		$fraze=UserFraze::where(array('user_id'=>$this->user['id'],'module_id'=>1))->find();
		if($fraze){
			$this->error('您的账号已被限制【发布宝贝功能】了,解封请联系在线客服');
		}
		if(empty($data['title'])){
			$this->error('请输入产品标题');		
		}
		if(empty($data['video_img'])){
			$this->error('请上传视频封面');		
		}
		if($data['price'] == '' || $data['price'] < 0){
			$this->error('请输入正确的产品价格');		
		}
		//积分超过2000 需要审核
		if($data['price'] > 2000){
			$data['status'] = 0;
		}
		if(empty($data['category_id'])){
			$this->error('请选择分类');		
		}
		if(!$line_1){
			if(empty($data['province'])/*  || empty($data['city']) || empty($data['area']) */ || $data['province'] == 'undefined' /* || $data['city'] == 'undefined'  || $data['area'] == 'undefined'  */){
				$this->error('请选择发货地区');		
			}
			if(empty($data['address'])){
				$this->error('请输入详细地址');		
			}
		}else{
			$yg = [
                'line_1'=>$line_1,
                'line_2'=>$line_2,
                'town'=>$town,
                'county'=>$county,
                'postcode'=>$postcode,
                'zhi'=>$zhi
            ];
            $data['yg'] = json_encode($yg,JSON_UNESCAPED_UNICODE);
		}
		
		if(empty($data['name'])){
			$this->error('请输入联系人姓名');
		}
		// if(empty($data['phone']) || !check_phone($data['phone'])){
		// 	$this->error('请输入有效的联系电话');
		// }
		$data['is_sale']		=	0;
		$category = ProductCategory::where(['id'=>$data['category_id']])->find();
		if(!$category){
			$this->error('分类错误');		
		}elseif($category['pid'] != 1){
			$data['category_id_tow'] = $category['id'];
			$data['category_id'] = $category['pid'];
		}

		//处理详细地址字段
		$address = explode('区',$data['address']);
		if(!empty($address[1])){
			$data['address'] = $address[count($address) - 1];
		}
		//添加成功后更新user表数据
		//User::where(['id'=>$this->user['id']])->update(['issue_name'=>$data['name'],'issue_phone'=>$data['phone'],'issue_address'=>$data['address'],'issue_province'=>$data['province'],'issue_city'=>$data['city'],'issue_area'=>$data['area']]);

		$data['add_time']		=	time();
		$data['day']		=	time();
		if($id){
			ProductModel::where(array('user_id'=>$this->user['id'],'id'=>$id))->update($data);
			$this->success('添加成功',['id'=>$id]);
		}else{
			$data['user_id']		=	$this->user['id'];
			$data['want_count'] = rand(1,5);
			$id=ProductModel::insertGetId($data);
			//查询今天是否已经发布成功过
			$redis = redis_init();
			$sb_time = $redis->get($this->user['id'].'roseandlog');	
		
			if(!$sb_time){
				//第一次发布给奖励
				roseandlog($data['user_id'],3,4,'发布闲置物品通过奖励');
				$redis->set($this->user['id'].'roseandlog',time());
			}else{
				//判断发布时间
				$time1 = date('Y-m-d',$sb_time);
				$time2 = date('Y-m-d',time());
				if($time1 != $time2){
					//上次发布不是今天 给奖励
					roseandlog($data['user_id'],3,4,'发布闲置物品通过奖励');
					$redis->set($this->user['id'].'roseandlog',time());
				}
			}
			if($is_prize && $data['status'] == 1){ //从参与抽奖里面过来发布闲置的
				$prize_user = PrizeUser::where(['user_id'=>$this->user['id'],'prize_id'=>$is_prize])->find();
				if($prize_user){ // 存在  增加发布闲置数量
					PrizeUser::where(['id'=>$prize_user['id']])->update(['num'=>$prize_user['num']+1]);
				}else{  //不存在添加数据
					PrizeUser::insert(['user_id'=>$this->user['id'],'prize_id'=>$is_prize,'add_time'=>time()]);
				}
			}
			
			$this->success('添加成功',['id'=>$id]);
		}
	}

	//我想要数量+1
	public function want_count_add(){
		$id = $this->request->request('id','');
		$info = ProductModel::where(['id'=>$id])->find();
		if($info){
			ProductModel::where(['id'=>$id])->update(['want_count'=>$info['want_count']+1]);
		}
	}

	
	
	//发布举报内容
	public function product_report_add(){
		$fraze=UserFraze::where(array('user_id'=>$this->user['id'],'module_id'=>6))->find();
		if($fraze){
			$this->error('您的账号已被限制【禁止提交举报内容】了,解封请联系在线客服');
		}
		$product_id	=	$this->request->request('product_id',0);
		$wish_id	=	$this->request->request('wish_id',0);
		$title		=	$this->request->request('title','');
		$user_id	=	$this->request->request('user_id',0);
		if($title){
			$data=array(
			    'product_id'=>$product_id,
			    'wish_id'	=>$wish_id,
			    'title'     =>$title,
			    'user_id'   =>$user_id,
			    'status'    =>1,
			    'add_time'  =>time()
			);
			ProductReport::insert($data);
			$this->success('举报成功,等待工作人员审核');
		}else{
			$this->error('举报失败');
		}
	}
	//删除自己的留言
	public function product_message_del(){
		$message_id	=	$this->request->request('message_id',0);
		if($message_id){
			$message=ProductMessage::where(array('id'=>$message_id))->find();
			if($message['user_id']==$this->user['id']){
				ProductMessage::where(array('id'=>$message_id))->delete();
				$message_count = ProductModel::where(array('id'=>$message['product_id']))->value('message_count');
			    ProductModel::where(array('id'=>$message['product_id']))->update(['message_count'=>$message_count-1]);
				$this->success('删除成功');
			}else{
				$this->error('您只能删除自己的留言');
			}
		}else{
			$this->error('缺少参数');
		}
	}
	//发布留言
	public function product_message_add(){
	    $fraze=UserFraze::where(array('user_id'=>$this->user['id'],'module_id'=>2))->find();
		if($fraze){
			$this->error('您的账号已被限制【发布留言评论功能】了,解封请联系在线客服');
		}
		$product_id	=	$this->request->request('product_id',0);
		$wish_id	=	$this->request->request('wish_id',0);
		$title		=	$this->request->request('title','');
		$message_id	=	$this->request->request('message_id',0);
		$user_id	=	$this->request->request('user_id',0);
		if(($product_id || $wish_id) && $title){
			$type	=	$message_id>0?1:0;
			if($type==1){		
				$to_user_id=ProductMessage::where(array('id'=>$message_id))->value('user_id');		//消息发给评论者
			}else{
				if($product_id){
					$to_user_id=ProductModel::where(array('id'=>$product_id))->value('user_id');	//消息发给商品发布者		
				}else{
					$to_user_id=ProductWish::where(array('id'=>$wish_id))->value('user_id');	//消息发给商品发布者		
				}
			}
			if($user_id){
				$to_user_id = $user_id;
			}
			$data=array(
			    'product_id'=>$product_id,
			    'wish_id'	=>$wish_id,
			    'parent_id' =>$message_id,
			    'title'     =>$title,
			    'user_id'   =>$this->user['id'],
			    'type'      =>$type,
			    'to_user_id'=>$to_user_id,
			    'is_read'   =>1,
			    'status'    =>1,
			    'add_time'  =>time()
			);
			$id=ProductMessage::insert($data);
			if($message_id==0){
				$message_count = ProductModel::where(array('id'=>$product_id))->value('message_count');
			    ProductModel::where(array('id'=>$product_id))->update(['message_count'=>1+$message_count]);//累计留言数量
			}
			User::where(array('id'=>$to_user_id))->update(array('read_message'=>1));//有未读消息
			$data['id']=$id;
			$data['user']=User::field('id,avatar,nickname')->where(array('id'=>$this->user['id']))->find();
			$data['is_love']=0;
			$data['love_count']=0;
			$data['del']=1;
			$data['answer']=[];
			$data['add_time']='刚刚';
			$this->success('留言成功',['data'=>$data]);
		}else{
			$this->error('留言不能为空');
		}
	}
	//商品评论列表
	public function product_message(){
        $page = $this->request->request('page',1);
        $pagesize = $this->request->request('pagesize',10);
        $product_id = $this->request->request('product_id',0);
        $wish_id = $this->request->request('wish_id',0);
		if(!$product_id && !$wish_id){
            $this->error('参数无效');
		}
		$where=array('product_id'=>$product_id,'wish_id'=>$wish_id,'status'=>1);
	    $where['type']=array('in',array(0,1));
		//$total  = ProductMessage::where($where)->count();
	    $where['parent_id']=0;
		$list=ProductMessage::where($where)->order('add_time desc')->limit(($page-1)*$pagesize,$pagesize)->select();
		foreach($list as $a=>&$b){
			$b['add_time']=timeago($b['add_time']);
			$b['imgs']=$b['imgs']?explode(',',$b['imgs']):[];
			$b['user']	=User::field('id,avatar,nickname')->where(array('id'=>$b['user_id']))->find();
			$b['is_love']=0;
			$answer=ProductMessage::where(array('parent_id'=>$b['id'],'status'=>1))->order('add_time desc')->select();
			if($answer){
				foreach ($answer as &$b_1) {
					$b_1['add_time']=timeago($b_1['add_time']);
					$b_1['user']=User::field('id,avatar,nickname')->where(array('id'=>$b_1['user_id']))->find();
					$b_1['is_love']=0;
					$answer1=ProductMessage::where(array('parent_id'=>$b_1['id'],'status'=>1))->order('add_time desc')->select();
					if($answer1){
						foreach ($answer1 as &$b_2) {
							$b_2['add_time']=timeago($b_2['add_time']);
							$b_2['user']=User::field('id,avatar,nickname')->where(array('id'=>$b_2['user_id']))->find();
							$b_2['is_love']=0;
						}
					}
					$b_1['answer'] = $answer1;
				}
			}
			$b['answer']=$answer;
		}
        $this->success('获取成功',['list'=>$list,'total'=>count($list)]);
	}
	//发布心愿
	public function product_wish_save(){
		$id			            = $this->request->request('product_wish_id','');
		//$data['category_id']	= $this->request->request('category_id','');
		$data['title']			= $this->request->request('title','');
		$data['description']			= $this->request->request('description','');
		$data['exchange_title']			= $this->request->request('exchange_title','换万物');
		$data['province']		= $this->request->request('province','');
		$data['city']			= $this->request->request('city','');
		$data['area']			= $this->request->request('area','');
		$data['address']			= $this->request->request('address','');
		$data['longitude']		= $this->request->request('longitude',0);
		$data['latitude']		= $this->request->request('latitude',0);
		$data['imgs']			= $this->request->request('imgs','');
		$data['status']         = $this->request->request('status',0);
		$data['video_img']		= $this->request->request('video_img','');
		$data['name']		= $this->request->request('name','');
		$data['phone']		= $this->request->request('phone','');
		if(!$data['imgs'] || $data['imgs'] == 'undefined'){
			$data['imgs'] = getWishImg($data);
		}
		if(!$data['video_img'] || $data['video_img'] == 'undefined'){
			$data['video_img'] = $data['imgs'];
		}
		// if(!$data['longitude'] || !$data['latitude']){
		// 	$fid = SArea::where(['name'=>['like','%'.$data['province'].'%']])->value('id');
		// 	if($data['city'] != '市辖区'){
		// 		$fid = SArea::where(['name'=>['like','%'.$data['city'].'%'],'fid'=>$fid])->value('id');
		// 	}
		// 	$info = SArea::where(['name'=>['like','%'.$data['area'].'%'],'fid'=>$fid])->find();
		// 	$data['longitude'] = $info['lng'];
		// 	$data['latitude'] = $info['lat'];
		// }
		$fraze=UserFraze::where(array('user_id'=>$this->user['id'],'module_id'=>1))->find();
		if($fraze){
			$this->error('您的账号已被限制【发布宝贝功能】了,解封请联系在线客服');
		}
		if(empty($data['title'])){
			$this->error('请输入产品标题');		
		}
		// if(empty($data['category_id'])){
		// 	$this->error('请选择分类');		
		// }
		// if(empty($data['province']) || empty($data['city']) || empty($data['area']) ){
		// 	$this->error('请选择发货地区');		
		// }
		$data['is_sale']		=	0;

		if($id){
			ProductWish::where(array('user_id'=>$this->user['id'],'id'=>$id))->update($data);
			$this->success('添加成功',['id'=>$id]);
		}else{
			$data['user_id']		=	$this->user['id'];
			$data['add_time']		=	time();
			$id=ProductWish::insert($data);
			//查询今天是否已经发布成功过
			$redis = redis_init();
			$sb_time = $redis->get($this->user['id'].'ProductWish');	
			if(!$sb_time){
				//第一次发布给奖励
				roseandlog($data['user_id'],1,4,'发布心愿物品通过奖励');
				$redis->set($this->user['id'].'ProductWish',time());
			}else{
				//判断发布时间
				$time1 = date('Y-m-d',$sb_time);
				$time2 = date('Y-m-d',time());
				if($time1 != $time2){
					//上次发布不是今天 给奖励
					roseandlog($data['user_id'],1,4,'发布心愿物品通过奖励');
					$redis->set($this->user['id'].'ProductWish',time());
				}
			}
			$this->success('添加成功',['id'=>$id]);
		}
	}
	//商品列表
	public function products(){
       
        $lng = $this->request->request('longitude');
        $lat = $this->request->request('latitude');
        $score = $this->request->request('score');
        $category_id = $this->request->request('category_id');
        $search_category_id = $this->request->request('search_category_id');  //分类页面跳进搜索页面的筛选
        $city = $this->request->request('city');
        $orderby = $this->request->request('orderby',1);
        $autocity = $this->request->request('autocity','');
        $guize = $this->request->request('guize',0);
        $wd = $this->request->request('wd','');
        $type_status = $this->request->request('type_status','');
        $page = $this->request->request('page',1);
        $pagesize = $this->request->request('pagesize',10);
		$token = $this->request->request('token','');

		$where = [
            'status' => 1,
            'is_sale' => 0
        ];
		if($token){
			$user=User::where(array('token'=>$token))->find();
			if($user){
				User::where('id',$user['id'])->update(['last_login_time'=>time()]);
				$userIds = UserReport::where(['user_id'=>$user['id'],'by_user_id'=>['>',0]])->column('by_user_id');
				if($userIds){
					$where['user_id'] = ['not in',$userIds];
				}
				$productIds = UserReport::where(['user_id'=>$user['id'],'by_user_id'=>0,'product_id'=>['>',0]])->column('product_id');
				if($productIds){
					$where['id'] = ['not in',$productIds];
				}
			}
		}
		if($category_id){
		    $subcate=ProductCategory::where(array('pid'=>$category_id))->column('id');
			$subcate[]=$category_id;
			$where['category_id']=array('in',$subcate);
		}
		// if($city&& $city!='全国'){
	    //     $where['city']=array('like','%'.$city.'%');
		// }
		switch ($orderby) {
            case 2://离我最近
				$order='distance asc';
                break;
            case 3://精选
                $order='recommend_time desc';

				if(!$lat){
					if($guize == 1 || $guize == 2){ 
						$order='recommend_time desc';
					}elseif($guize == 3){  //最新
						$order='add_time desc';
					}
				}
				
                break;
            case 4://最新发布
                $order='add_time desc';
                break;
			case 5://去白拿
				$order='add_time desc';
				$where['price'] = ['<=',$score];
				break;
            default:
                $order='add_time desc';
                break;
        }

		if($type_status == 2){
			$type_status = 3;
			$order='add_time desc';
		}else{
			$type_status = 1;
		}
		$where['type_status'] = $type_status;
		$where1 = [];
		if(!empty($wd)){
			if(!empty($search_category_id)){
				$where1 = $where;
				$category=ProductCategory::where(array('id'=>$search_category_id))->find();
				if($category['pid'] == 1){ //一级分类
					$where1['category_id'] = $category['id'];
				}else{ // 二级分类
					$where1['category_id'] = $category['pid'];
					$where1['category_id_tow'] = $category['id'];
				}
			}
			$where['title']=array('like','%'.$wd.'%');
		}
		if($lat && $orderby == 2){
	    	$list=ProductModel::field("*,ACOS(
	        	COS(RADIANS($lat)) *
	        	COS(RADIANS(latitude)) *
	        	COS(RADIANS(longitude) - RADIANS($lng)) +
	        	SIN(RADIANS($lat)) *
	        	SIN(RADIANS(latitude))
            	) * 6378 as distance")->where($where)->whereOr(function($query) use ($where1){
					$query->where($where1);
				 })->order($order)->limit(($page-1)*$pagesize,$pagesize)->select();
		}elseif($lat &&  $orderby == 3){
			$where['recommend_time'] = ['>',0];
			$order = 'distance asc';
			if($guize == 1){ //最近
				$order='recommend_time desc';
			}elseif($guize == 2){ //最近
				$order='distance asc';
			}elseif($guize == 3){  //最新
				$order='add_time desc';
			}
	    	$list=ProductModel::field("*,ACOS(
	        	COS(RADIANS($lat)) *
	        	COS(RADIANS(latitude)) *
	        	COS(RADIANS(longitude) - RADIANS($lng)) +
	        	SIN(RADIANS($lat)) *
	        	SIN(RADIANS(latitude))
            	) * 6378 as distance")->where($where)->whereOr(function($query) use ($where1){
					$query->where($where1);
				 })->order($order)->limit(($page-1)*$pagesize,$pagesize)->select();
			if(!$list){
				$where['recommend_time'] = 0;
				$list=ProductModel::field("*,ACOS(
					COS(RADIANS($lat)) *
					COS(RADIANS(latitude)) *
					COS(RADIANS(longitude) - RADIANS($lng)) +
					SIN(RADIANS($lat)) *
					SIN(RADIANS(latitude))
					) * 6378 as distance")->where($where)->whereOr(function($query) use ($where1){
						$query->where($where1);
					 })->order($order)->limit(($page-1)*$pagesize,$pagesize)->select();
	
			}
		}else{
    		$list=ProductModel::where($where)->whereOr(function($query) use ($where1){
				$query->where($where1);
			 })->order($order)->limit(($page-1)*$pagesize,$pagesize)->select();
		}
		
		//去白拿 随机给超出金额的商品
		if($orderby == 5 && $page == 1){
			if($user){
				$where = [
					'price'=>['>',$user['rose_money'] + 19],
					'status' => 1,
            		'is_sale' => 0
				];
				$info = ProductModel::where($where)->order('type asc , price asc')->limit(0,2)->select();
				if($info){
					if(count($info) == 1){
						$list[1] = $info[0];
					}else{
						$list[1] = $info[0];
						$list[4] = $info[1];
					}
				}
			}
		}

		foreach($list as $a=>&$b){
			if($b['want_count'] == 0){
				$b['want_count'] = rand(1,5);
				ProductModel::where(['id'=>$b['id']])->update(['want_count'=>$b['want_count']]);
			}
		    $b['imgsa']=explode(',',$b['imgs']);
			$b['add_time']=timeago($b['add_time']);
			if(!$b['video_img']){
				$b['video_img']=$b['imgsa'][0];
			}
			if(strstr($b['video_img'], 'mp4')){
				$b['video_img'] = $b['video_img'].'?x-oss-process=video/snapshot,t_1000,m_fast,ar_auto';
			}

			$res= explode('oa.yuxiang123.cn/',$b['video_img']);
			if (!empty($res[1]) &&  !file_exists($res[1])) {
				unset($list[$a]);
			} 

		

			$b['wand'] = rand(1,10);
			$b['is_local']=$orderby == 2 ? 1 : 0;
			$b['is_fans']=0;
			$b['is_love']=0;
			$b['is_collect']=0;
			$b['state']='pause';//初始状态标志（不改）默认pause
			$b['isShowProgressBarTime']=false;//是否显示进度条 默认false
			$b['isplay']=false;//是否播放音频 默认true
			$b['user']	=User::field('id,avatar,nickname')->where(array('id'=>$b['user_id']))->find();
			$b['message_count']=ProductMessage::where(array('product_id'=>$b['id'],'type'=>array('in',array(0,1)),'status'=>1))->count();
			if(empty($b['longitude']) || $b['longitude'] == 'undefined' || empty($b['latitude']) || $b['latitude'] == 'undefined'){
				if($b['province']){
					$fid = SArea::where(['name'=>['like','%'.$b['province'].'%']])->value('id');
					if($fid == 1 && $b['city'] == '北京市'){
						if($b['area']){
							$info = SArea::where(['name'=>['like','%'.$b['area'].'%'],'fid'=>1])->find();
						}else{
							$info = SArea::where(['name'=>['like','%'.$b['province'].'%']])->find();
						}
					}else{
						if($b['city']){
							if($b['city'] != '市辖区'){
								if($b['city'] == '省直辖县级行政区划') $b['city'] = '直辖县级';
								$fid_1 = SArea::where(['name'=>['like','%'.$b['city'].'%'],'fid'=>$fid])->value('id');
								if($b['area']){
									$info = SArea::where(['name'=>['like','%'.$b['area'].'%'],'fid'=>$fid_1])->find();
								}
							}
							if(empty($info)){
								$info = SArea::where(['name'=>['like','%'.$b['city'].'%'],'fid'=>$fid])->find();
							}
						}else{
							$info = SArea::where(['name'=>['like','%'.$b['province'].'%']])->find();
						}
					}
					if(!empty($info)){
						$b['longitude'] = $info['lng'];
						$b['latitude'] = $info['lat'];
						//更新用户坐标
						ProductModel::where(['id'=>$b['id']])->update(['longitude'=>$b['longitude'],'latitude'=>$b['latitude']]);
					}

				}
			}
			if($lng && $b['longitude']){
                $b['juli']=juli(getDistance($lat,$lng,$b['latitude'],$b['longitude']));
			}else{
			    $b['juli']='';
			}
            $b['city']=str_replace('Array','',$b['city']);
			if($b['city'] == '市辖区' || $b['city'] == '直辖县级'  || $b['city'] == '省直辖县级行政区划'){
				$b['city'] = $b['province'];
			}
		}
		$goods_num = 0;
		if($score){
			$where = [];
			$where['status']=1;
			$where['is_sale']=0;
			$where['price']=['elt',$score];
			$goods_num =ProductModel::where($where)->count();
		}
		$id = Prize::where(['status'=>0])->value('id');
		if($id){
			$is_prize = 0;
			if(!empty($user)){
				//查询用户是否已经参与了抽奖
				$info = PrizeUser::where(['prize_id'=>$id,'user_id'=>$user['id']])->find();
				if($info){
					$is_prize = 0;
				}
			}
		}else{
			$is_prize = 0;
		}
		$list = array_values($list);
        $this->success('获取成功',['list'=>$list,'goods_num'=>$goods_num,'is_prize'=>$is_prize]);
	}

	//查询用户是否抽奖
	public function isPrize(){
		$token = $this->request->request('token','');
		if($token){
			$user=User::where(array('token'=>$token))->find();
		}
		$id = Prize::where(['status'=>0])->value('id');
		if($id){
			$is_prize = 1;
			if(!empty($user)){
				//查询用户是否已经参与了抽奖
				$info = PrizeUser::where(['prize_id'=>$id,'user_id'=>$user['id']])->find();
				if($info){
					$is_prize = 0;
				}
			}
		}else{
			$is_prize = 0;
		}
        $this->success('获取成功',$is_prize);
	}


	//获取关键词分类
	public function getKeyword(){
		$keyword = $this->request->request('keyword','');
		$listArr = [];
		if($keyword){
			$list = ProductCategory::where(['text'=>['neq','']])->select();
			foreach ($list as $v) {
				$arr = explode(' ',$v['text']);
				foreach ($arr as $vo) {
					if($vo && strpos($keyword, $vo) !== false){
						$listArr[] = $v;
						break;
					}
				}
			}
		}
		$listArr[] = ProductCategory::where(['name'=>'其他'])->find();
		$this->success('获取成功',$listArr);
	}

	//搜人名
	public function getUserProduct(){
        $wd = $this->request->request('wd','');
        $token = $this->request->request('token','');
        $page = $this->request->request('page',1);
        $pagesize = $this->request->request('pagesize',10);

		$user = [];
		if($token){
			$user=User::where(array('token'=>$token))->find();
		}
		$where = [
			'nickname|mobile'=> ['like','%'.$wd.'%'],
			'status'=>1
		];

		if(!empty($this->user)){
			$userIds = UserReport::where(['user_id'=>$this->user['id'],'by_user_id'=>['>',0]])->column('by_user_id');
			if($userIds){
				$where['id'] = ['not in',$userIds];
			}
		}

		$list = User::where($where)->limit(($page-1)*$pagesize,$pagesize)->select();
		foreach ($list as &$v) {
			//总共发布的闲置
			$v['product_sum'] = ProductModel::where(['user_id'=>$v['id'],'status'=>1])->count();
			//售出的闲置
			$v['product_sell'] = ProductModel::where(['user_id'=>$v['id'],'status'=>1,'is_sale'=>1])->count();
			$v['product'] = ProductModel::where(['user_id'=>$v['id'],'status'=>1,'is_sale'=>0,'is_del'=>0])->limit(0,10)->select();
			//来到余享多少天
			$data1 = date_create(date('Y-m-d',$v['add_time']));
			$data2 = date_create(date('Y-m-d',time()));
			$later  = date_diff($data1,$data2);
			$v['sky']  = $later->format("%a");
			$v['add_time']  = date('Y-m-d',$v['add_time']);

			$v['is_follow'] = 0;
			if($user){
				$follow=Follow::where(array('fans_user_id'=>$user['id'],'user_id'=>$v['id']))->find();
				$v['is_follow']=$follow?1:0;
			}
		}
		$this->success('获取成功',['list'=>$list]);
	}
	//心愿列表
	public function products_wish(){
        $lng = $this->request->request('longitude');
        $lat = $this->request->request('latitude');
        $orderby = $this->request->request('orderby',1);
        $page = $this->request->request('page',1);
        $pagesize = $this->request->request('pagesize',10);
        $where = [
            'status' => 1,
            'is_sale' => 0
        ];

		if(!empty($this->user)){
			$userIds = UserReport::where(['user_id'=>$this->user['id'],'by_user_id'=>['>',0]])->column('by_user_id');
			if($userIds){
				$where['user_id'] = ['not in',$userIds];
			}
			$productIds = UserReport::where(['user_id'=>$this->user['id'],'by_user_id'=>0,'wish_id'=>['>',0]])->column('wish_id');
			if($productIds){
				$where['id'] = ['not in',$productIds];
			}
		}
        switch ($orderby) {
            case 3://精选
				$order='add_time desc';
				$where['is_new'] = 1;
                //$order='recommend_time desc,add_time desc';
                break;
            case 4://最新发布
                $order='add_time desc';
				$where['is_new'] = 0;
                break;
			case 5://去白拿
				$order='add_time desc';
				$where['price'] = ['<=',$this->user['rose_money']];
				break;
            default:
                $order='add_time desc';
                break;
        }
		if($lat){
	    	$list=ProductWish::field("*,ACOS(
	        	COS(RADIANS($lat)) *
	        	COS(RADIANS(latitude)) *
	        	COS(RADIANS(longitude) - RADIANS($lng)) +
	        	SIN(RADIANS($lat)) *
	        	SIN(RADIANS(latitude))
            	) * 6378 as distance")->where($where)->order($order)->limit(($page-1)*$pagesize,$pagesize)->select();
		}else{
    		$list=ProductWish::where($where)->order($order)->limit(($page-1)*$pagesize,$pagesize)->select();
		}
		foreach($list as $a=>&$b){
			if(!$b['imgs']){
				$b['imgs'] = getWishImg($b);
			}
		    $b['imgsa']=explode(',',$b['imgs']);
			if(!$b['video_img']){
				$b['video_img']=$b['imgsa'][0];
			}
			if(strstr($b['video_img'], 'mp4')){
				$b['video_img'] = $b['video_img'].'?x-oss-process=video/snapshot,t_1000,m_fast,ar_auto';
			}
		    $b['wand']= rand(1,10);
			$b['user']= User::field('id,avatar,nickname')->where(array('id'=>$b['user_id']))->find();
			if($lng && $b['longitude']){
                $b['juli']=juli(getDistance($lat,$lng,$b['latitude'],$b['longitude']));
			}else{
			    $b['juli']='';
			}
            $b['city']=str_replace('Array','',$b['city']);
			if($b['city'] == '市辖区'){
				$b['city'] = $b['province'];
			}
			$b['add_time'] = date('Y-m-d H:i:s',$b['add_time']);
		}
        $this->success('获取成功',$list);
	}
	//心愿详情
	public function product_wish_detail(){
		$id = $this->request->request('id');
		$info = ProductWish::where(['id'=>$id])->find();
		if(!$info) $this->error('参数错误');
		if(!$info['imgs']){
			$info['imgs'] = [getWishImg($info)];
		}else{
			$info['imgs'] = explode(',',$info['imgs']);
		}
		$info['category_name']=Category::where(array('id'=>$info['category_id']))->value('name');
		$info['user'] = User::where(['id'=>$info['user_id']])->find();
		$info['user']['add_time'] = date('m-d H:i',$info['user']['add_time']);
		$follow=Follow::where(array('fans_user_id'=>$this->user['id'],'user_id'=>$info['user_id']))->find();
		$info['is_follow']=$follow?1:0;

		$this->success('获取成功',$info);
	}

	//查最新三条商品状态
	public function product_cash(){
		$where = [
			'user_id'=>$this->user['id'],
			'is_del'=>0,
			'add_time'=>['>',1692076252]
		];
		$list=ProductModel::where($where)->order('add_time desc')->limit(0,3)->select();
		foreach ($list as &$v) {
			if($v['status'] == 0){
				$v['status'] = '待领取';
			}elseif($v['status'] == 1){
				$v['status'] = '已领取';
			}elseif($v['status'] == -1){
				$v['status'] = '已失效';
			}		
		}
 		$this->success('获取成功',$list);
	}

	//聊天也获取我的闲置信息
	public function get_my_product(){
		$user_id=$this->request->request('user_id');
		if($user_id == 439){
			$where = ['status'=>1,'is_sale'=>0];
		}else{
			$where = ['user_id'=>$user_id,'status'=>1,'is_sale'=>0];
		}
		//发布闲置
		$my_product=ProductModel::field('id,video_img,title,price,imgs')->where($where)->limit(0,50)->order('add_time desc')->select();
		if($my_product){
    		foreach($my_product as $a=>&$b){
				if(!$b['imgs']){
					$b['imgs'] = getWishImg($b);
				}
				$imgsa=explode(',',$b['imgs']);
				if(!$b['video_img']){
					$b['video_img']=$imgsa[0];
				}
				if(strstr($b['video_img'], 'mp4')){
					$b['video_img'] = $b['video_img'].'?x-oss-process=video/snapshot,t_1000,m_fast,ar_auto';
				}
				unset($b['imgs']);
	    	}
		}else{
		    $my_product=[];
		}
		$this->success('获取成功',$my_product);
	}

	//我的发布
	public function my_product(){
		$user_id=$this->request->request('user_id');
		$page=$this->request->request('page',1);
		$status=$this->request->request('status','');
		$is_admin=$this->request->request('is_admin','');
		$type=$this->request->request('type',1);
		$field=$this->request->request('field','*');
		$pagesize=$this->request->request('pagesize',100);		
		$pagesize = 100;
        $lng=$this->request->request('longitude');
        $lat=$this->request->request('latitude');
		if(!$is_admin){
			$where = ['user_id'=>$user_id];
			if($status){
				$where['status'] = $status;
			}
		}else{
			$where = ['is_sale'=>0];
		}
		
		if($type == 1){  
			//发布闲置
			$my_product=ProductModel::field($field)->where($where)->limit(($page-1)*$pagesize,$pagesize)->order('add_time desc')->select();
		}elseif($type == 2){
			//发布动态
			$my_product=Community::field($field)->where($where)->limit(($page-1)*$pagesize,$pagesize)->order('add_time desc')->select();
		}elseif($type == 3){
			//发布心愿
			$my_product=ProductWish::field($field)->where($where)->limit(($page-1)*$pagesize,$pagesize)->order('add_time desc')->select();
		}else{
			$where = ['user_id'=>$this->user['id']];
			$my_product=ProductTake::field($field)->where($where)->limit(($page-1)*$pagesize,$pagesize)->order('add_time desc')->select();
		}
		if($my_product){
    		foreach($my_product as $a=>&$b){
				if($type == 2){
					if(!$b['imgs']){
						$b['imgs'] = 'https://oa.yuxiang123.cn/uploads/shequ.png';
					}
					$b['imgsa']=explode(',',$b['imgs']);
					$b['video_img']=$b['imgsa'][0];
					$b['title'] = $b['content'];
					$b['add_time']=timeago($b['add_time']);
					$b['user']	=User::field('id,avatar,nickname')->where(array('id'=>$b['user_id']))->find();
				}elseif($type == 4){
					if(!$b['imgs']){
						$b['imgs'] = 'https://oa.yuxiang123.cn/uploads/shequ.png';
					}
					$b['imgsa']=explode(',',$b['imgs']);
					$b['video_img']=$b['imgsa'][0];
					if(strstr($b['video_img'], 'mp4')){
						$b['video_img'] = $b['video_img'].'?x-oss-process=video/snapshot,t_1000,m_fast,ar_auto';
					}
				}else{
					if(!$b['imgs']){
						$b['imgs'] = getWishImg($b);
					}
					$b['imgsa']=explode(',',$b['imgs']);
					if(!$b['video_img']){
						$b['video_img']=$b['imgsa'][0];
					}
					if(strstr($b['video_img'], 'mp4')){
						$b['video_img'] = $b['video_img'].'?x-oss-process=video/snapshot,t_1000,m_fast,ar_auto';
					}
					

					$b['add_time']=timeago($b['add_time']);
					$b['user']	=User::field('id,avatar,nickname')->where(array('id'=>$b['user_id']))->find();
					$b['wand'] = rand(1,10);
					// //是否关注
					// $fans=Follow::field('id')->where(array('fans_user_id'=>$this->user['id'],'user_id'=>$b['user_id']))->find();
					// $b['is_fans']=$fans?1:0;	
					// //是否喜欢
					// $love=M('love')->field('id')->where(array('product_id'=>$b['id'],'user_id'=>$this->user['id']))->find();
					// $b['is_love']=$love?1:0;	
					// //是否收藏
					// $collect=M('collect')->field('id')->where(array('product_id'=>$b['id'],'user_id'=>$this->user['id']))->find();
					// $b['is_collect']=$collect?1:0;
					$b['message_count']=ProductMessage::where(array('product_id'=>$b['id'],'status'=>1,'type'=>array('in',array(0,1))))->count();
					if($lat=="" || $b['latitude']){
						$b['juli']='';
					}else{
						$b['juli']=juli(getDistance($lat,$lng,$b['latitude'],$b['longitude']));
					}
				}
				$res= explode('oa.yuxiang123.cn/',$b['video_img']);
				if (!empty($res[1]) && !file_exists($res[1])) {
					unset($my_product[$a]);
				} 
				
	    	}
			$my_product = array_values($my_product);
		}else{
		    $my_product=[];
		}
		$this->success('获取成功',$my_product);
	}


	//官方售卖
	public function official_product(){
		$page=$this->request->request('page',1);
		$field=$this->request->request('field','*');
		$pagesize=$this->request->request('pagesize',18);		
		$where = ['is_sale'=>0,'user_id'=>439,'status'=>1,'type_status'=>1];
		//发布闲置
		$my_product=ProductModel::field($field)->where($where)->limit(($page-1)*$pagesize,$pagesize)->order('add_time desc')->select();
		if($my_product){
    		foreach($my_product as $a=>&$b){
				if(!$b['imgs']){
					$b['imgs'] = getWishImg($b);
				}
				$b['imgsa']=explode(',',$b['imgs']);
				if(!$b['video_img']){
					$b['video_img']=$b['imgsa'][0];
				}
				$res= explode('oa.yuxiang123.cn/',$b['video_img']);
				if (!empty($res[1]) && !file_exists($res[1])) {
					unset($my_product[$a]);
				} 
				if(strstr($b['video_img'], 'mp4')){
					$b['video_img'] = $b['video_img'].'?x-oss-process=video/snapshot,t_1000,m_fast,ar_auto';
				}
				$b['add_time']=timeago($b['add_time']);
				$b['user']	=User::field('id,avatar,nickname')->where(array('id'=>$b['user_id']))->find();
				$b['wand'] = rand(1,10);
				$b['message_count']=ProductMessage::where(array('product_id'=>$b['id'],'status'=>1,'type'=>array('in',array(0,1))))->count();
				
	    	}
		}else{
		    $my_product=[];
		}
		$my_product = array_values($my_product);
		$this->success('获取成功',$my_product);
	}

	public function product_del(){
		$product_id=$this->request->request('product_id',0);
		$type=$this->request->request('type',1);
		if($type == 1){
			$model = new ProductModel();
		}elseif($type == 2){
			$model = new Community();
		}else{
			$model = new ProductWish();
		}
		if($product_id){
			$product=$model->where(array('id'=>$product_id))->find();
			if(!$product){
				$this->success('删除成功');
			}
			if($product['user_id']==$this->user['id'] || $this->user['is_admin'] == 1){
				if($type == 1){
					$order=UserOrder::where(array('product_id'=>$product_id,'status'=>array('gt',0)))->order('status asc')->limit(0,1)->find();				
					if($order ){
						$this->error('商品在交易中,不能删除');
					}
					UserOrder::where(array('product_id'=>$product_id,'status'=>0))->delete();
					ProductViewLog::where(array('product_id'=>$product_id))->delete();
				}
				$model->where(array('id'=>$product_id))->delete();
				$this->success('删除成功');
			}else{
				$this->error('不是您的商品，不能删除');
			}
		}else{
			$this->error('缺少参数');
		}	
	}
	//上架/下架
	public function product_down(){
	    $product_id=$this->request->request('product_id',0);
		$type=$this->request->request('type',1);
		$status=$this->request->request('status','-1');
		if($type == 1){
			$model = new ProductModel();
		}else{
			$model = new ProductWish();
		}

		if($product_id){
			$product=$model->where(array('id'=>$product_id))->find();
			if(!$product){
				$this->error('该商品已删除');
			}
			if($product['user_id']==$this->user['id'] || $this->user['is_admin'] == 1){
			    $model->where(array('id'=>$product_id))->update(array('status'=>$status));
				$this->success('操作成功');
			}
		}
		$this->error('操作失败');

	}

	//闲置详情
	public function product_detail(){
        $product_id = $this->request->request('product_id');
		$lng=$this->request->request('longitude','');
        $lat=$this->request->request('latitude','');
        $token=$this->request->request('token','');

		$product=ProductModel::where(array('id'=>$product_id))->find();
		if(!$product){
			$this->error('参数错误');
		}
		$product['category_name']=ProductCategory::where(array('id'=>$product['category_id']))->value('name');
		$product['user']=User::where(array('id'=>$product['user_id']))->find();
		$product['add_time'] = date('m-d H:i',$product['add_time']);
		$product['xcx_qun_ewm']=ConfigData::where(array('name'=>'xcx_qun_ewm'))->value('value');
		if($lat && $product['latitude']){
            $product['juli']=juli(getDistance($lat,$lng,$product['latitude'],$product['longitude']));
        }else{
            $product['juli']='';//定位失败
        }
		$product['imgs']=explode(',',$product['imgs']);
		$follow = 0;
		$collect = 0;
		if($token){
			$this->user=User::where(array('token'=>$token))->find();
			$follow=Follow::where(array('fans_user_id'=>$this->user['id'],'user_id'=>$product['user_id']))->find();
			$collect=UserCollect::where(array('user_id'=>$this->user['id'],'product_id'=>$product['id'],'type'=>1))->find();
		}
		$product['collect']=$collect?1:0;
		$product['is_follow']=$follow?1:0;

		//获取用户信息
		//总共发布的闲置
		$product['product_sum'] = ProductModel::where(['user_id'=>$product['user_id'],'status'=>1])->count();
		//售出的闲置
		$product['product_sell'] = ProductModel::where(['user_id'=>$product['user_id'],'status'=>1,'is_sale'=>1])->count();
		$product['user_product'] = ProductModel::where(['user_id'=>$product['user_id'],'status'=>1,'is_sale'=>0,'is_del'=>0,'type_status'=>$product['type_status']])->limit(0,10)->select();
		//来到余享多少天
		$data1 = date_create(date('Y-m-d',$product['user']['add_time']));
		$data2 = date_create(date('Y-m-d',time()));
		$later  = date_diff($data1,$data2);
		$product['sky']  = $later->format("%a");
		$product['user']['add_time']  = date('Y-m-d',$product['user']['add_time'] );
		if($product['yg']){
			$product['yg'] = json_decode($product['yg'],true);
		}

		$participation_product = ParticipationProduct::alias('a')
					->field("a.status,b.nickname,a.add_time,b.avatar,a.user_id,a.id")
					->join("user b","b.id = a.user_id")
					->where(['product_id'=>$product['id'],'a.status'=>1])
					->select();
		if($participation_product){
			foreach ($participation_product as &$v) {
				$v['add_time'] = date('Y-m-d',$v['add_time']);
			}
			$product['participation_product'] = $participation_product;
		}else{
			$product['participation_product'] = '';
		}
		
		if($product['wx']){
			// 截取前两位
			$firstTwo = mb_substr($product['wx'], 0, 2, 'UTF-8');
			
			// 截取后两位
			$lastTwo = mb_substr($product['wx'], -2, 2, 'UTF-8'); 

			$product['wx_new'] = $firstTwo.'****'.$lastTwo;
		}
		if($product['qq']){
			// 截取前两位
			$firstTwo = mb_substr($product['qq'], 0, 2, 'UTF-8');
			
			// 截取后两位
			$lastTwo = mb_substr($product['qq'], -2, 2, 'UTF-8'); 

			$product['qq_new'] = $firstTwo.'****'.$lastTwo;
		}
		$product['is_my'] = 0;
		//查询是否存在排队
		$paiduiList = UserOrder::where(['product_id'=>$product['id'],'paidui'=>1,'status'=>2])->select();
		if($paiduiList){
			$product['paidui'] = 1;
			foreach ($paiduiList as &$v) {
				if($this->user['id'] == $v['user_id']){
					$product['is_my'] = 1;
					$product['order_id'] = $v['id'];
				}
				$v['user'] = User::where(['id'=>$v['user_id']])->find();
				$v['add_time'] = timeago($v['add_time']);
				
				//总共发布的闲置
				$v['product_sum'] = ProductModel::where(['user_id'=>$v['user']['id'],'status'=>1])->count();
				//售出的闲置
				$v['product_sell'] = ProductModel::where(['user_id'=>$v['user']['id'],'status'=>1,'is_sale'=>1])->count();
				//来到余享多少天
				$data1 = date_create(date('Y-m-d',$v['user']['add_time']));
				$data2 = date_create(date('Y-m-d',time()));
				$later  = date_diff($data1,$data2);
				$v['sky']  = $later->format("%a");
			}

			$product['paiduiList'] = $paiduiList;

		}

        $this->success('获取成功',$product);
	}


	public function user_product(){
        $product_id = $this->request->request('product_id');
        $product_ids = $this->request->request('product_ids');
		$product=ProductModel::where(['id'=>$product_id])->find();
		if(!$product){
			$this->error('参数错误');
		}
		$user_product = ProductModel::where(['id'=>['not in',explode(',',$product_ids)],'user_id'=>$product['user_id'],'status'=>1,'is_sale'=>0,'is_del'=>0,'type_status'=>$product['type_status']])->limit(0,10)->select();
        $this->success('获取成功',$user_product);
	}

	//收藏
	public function collect(){
		$id = $this->request->request('id');
		$where = [
			'product_id'=>$id,
			'user_id'=>$this->user['id'],
			'type'=>1
		];
		$info = UserCollect::where($where)->find();
		if($info){
			UserCollect::where($where)->delete();
		}else{
			$where['add_time'] = time();
			UserCollect::insert($where);
		}
		$this->success('操作成功');
	}

	//参与无偿分享领取
	public function participation_product(){
		$product_id =  $this->request->request('product_id');
		$info = ProductModel::where(['id'=>$product_id,'is_sale'=>0,'type_status'=>2])->find();
		if(!$info){
			$this->error('商品参数错误,无法参与无偿领取活动');
		}
		$info = ParticipationProduct::where(['product_id'=>$product_id,'user_id'=>$this->user['id']])->find();
		if($info){
			if($info['status'] == 1){
				$this->error('您已参与该件商品无偿领取活动,无法重复参与');
			}elseif($info['status'] == 2){
				$this->error('您已获得该件商品的无偿领取资格,无法重复参与');
			}else{
				$this->error('您未获得该件商品的无偿领取资格,请看看其他商品吧');
			}
		}else{
			ParticipationProduct::insert(['product_id'=>$product_id,'user_id'=>$this->user['id'],'add_time'=>time()]);
			$this->success('参与成功');
		}
	}
	//参与无偿分享领取
	public function set_participation_product(){
		$product_id =  $this->request->request('product_id');
		$id =  $this->request->request('id');

		$info = ParticipationProduct::where(['product_id'=>$product_id])->find();
		if($info && ($info['status'] == 2 || $info['status'] == -1)){
			$this->error('该商品已赠送,不可重复赠送');
		}else{
			$res = ParticipationProduct::where(['id'=>$id])->update(['status'=>2]);
			if($res){
				$product = ProductModel::where(['id'=>$product_id])->find();
				$address = UserAddress::where(['user_id'=>$info['user_id']])->order('is_default')->find();
				$data = [
					'user_id'=>$info['user_id'],
					'order_id'=>date("YmdHis").rand(1000,9999),
					'product_id'=>$product_id,
					'price'=>0,
					'money'=>$product['postage'],
					'ship_type'=>1,
					'shop_id'=>$product['user_id'],
					'add_time'=>time(),
					'total_money'=>0,
				];
				if($address){
					$data['address_username'] = $address['name'];
					$data['address_tel'] = $address['mobile'];
					$data['address_province'] = $address['province'];
					$data['address_city'] = $address['city'];
					$data['address_district'] = $address['district'];
					$data['address_detail'] = $address['address'];
				}
				UserOrder::insertGetId($data);
				$this->success('操作成功');
			}
		}

	}
	
	//相关产品推荐
	public function product_related(){
        $product_id = $this->request->request('product_id');
        $lng = $this->request->request('longitude','');
        $lat = $this->request->request('latitude','');
        $type_status = $this->request->request('type_status',1);
        $type = $this->request->request('type',1);  //type =1 是闲置   type=2 是心愿

		if($type == 1){
			$product=ProductModel::where(array('id'=>$product_id))->find();
			$where = ['category_id'=>$product['category_id'],'id'=>array('neq',$product_id),'status'=>1,'is_sale'=>0,'type_status'=>$type_status];

			if(!empty($this->user)){
				$userIds = UserReport::where(['user_id'=>$this->user['id'],'by_user_id'=>['>',0]])->column('by_user_id');
				if($userIds){
					$where['user_id'] = ['not in',$userIds];
				}
				$productIds = UserReport::where(['user_id'=>$this->user['id'],'by_user_id'=>0,'product_id'=>['>',0]])->column('product_id');
				if($productIds){
					$productIds[] = $product_id;
					$where['id'] = ['not in',$productIds];
				}
			}

			if($lat){
				$product_related=ProductModel::field("*,ACOS(
					COS(RADIANS($lat)) *
					COS(RADIANS(latitude)) *
					COS(RADIANS(longitude) - RADIANS($lng)) +
					SIN(RADIANS($lat)) *
					SIN(RADIANS(latitude))
					) * 6378 as distance")->where($where)->order('distance asc')->limit(0,6)->select();
			}else{
				$product_related=ProductModel::where($where)->order('add_time desc')->limit(0,6)->select();
			}
		}else{
			$product=ProductWish::where(array('id'=>$product_id))->find();
			$where = ['category_id'=>$product['category_id'],'id'=>array('neq',$product_id),'status'=>1];
			if(!empty($this->user)){
				$userIds = UserReport::where(['user_id'=>$this->user['id'],'by_user_id'=>['>',0]])->column('by_user_id');
				if($userIds){
					$where['user_id'] = ['not in',$userIds];
				}
				$productIds = UserReport::where(['user_id'=>$this->user['id'],'by_user_id'=>0,'wish_id'=>['>',0]])->column('wish_id');
				if($productIds){
					$productIds[] = $product_id;
					$where['id'] = ['not in',$productIds];
				}
			}
			$product_related=ProductWish::where($where)->limit(0,6)->select();
		}
		if($product_related){
    		foreach($product_related as $a=>&$b){
				if($type == 1 && !empty($this->user)){
					//查询是否被举报
					$UserReport = UserReport::where(['user_id'=>$this->user['id'],'product_id'=>$b['id']])->find();
					if($UserReport){
						unset($product_related[$a]);
					}
				}

				$b['wand'] = rand(1,10);
	    		$b['add_time']=timeago($b['add_time']);
				if(!$b['video_img']){
					$b['video_img'] = getWishImg($b);
				}
    		    $b['imgsa']=explode(',',$b['imgs']);
    			$b['state']='pause';//初始状态标志（不改）默认pause
    			$b['isShowProgressBarTime']=false;//是否显示进度条 默认false
    			$b['isplay']=false;//是否播放音频 默认true
	    		$b['user']	=User::field('id,avatar,nickname')->where(array('id'=>$b['user_id']))->find();
		    	//是否关注
			    $b['is_fans']=0;	
    			//是否喜欢
		    	$b['is_love']=0;	
			    //是否收藏
		    	$b['is_collect']=0;
                if($lat=="" || $b['latitude'] == ''){
                    $b['juli']='';
                }else{
                    $b['juli']=juli(getDistance($lat,$lng,$b['latitude'],$b['longitude']));
                }
                $b['zb']=$lat.','.$lng.','.$b['latitude'].','.$b['longitude'];

    			$b['message_count']=ProductMessage::where(array('product_id'=>$b['id'],'type'=>array('in',array(0,1)),'status'=>1))->count();
	    	}
			$product_related = array_values($product_related);
		}else{
		    $product_related=[];
		}
		$this->success('获取成功',$product_related);
	}


	

	//获取分享爱心证书海报
	public function get_share_hb()
	{
		//查询该用户总共分享过多少件闲置物品
		$share_num = ProductModel::where(['user_id'=>$this->user['id'],'status'=>1,'type_status'=>1])->count();
		if(!$share_num || $share_num < 3){
			$this->error('暂无闲置');
		}
        $bg="assets/qcode/bg.png";

		$appid='wx84f56a63b9f63e03';
	    $secret='98b698d6f873fdd2292684babd69c975';
		$res= file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret);
		$tokens=json_decode($res,true);
		if(empty($tokens['access_token'])){
		    $curl = curl_init("https://api.weixin.qq.com/cgi-bin/stable_token");
        	$parameters = json_encode(["grant_type"=>"client_credential", "appid"=>$appid,'secret'=>$secret]);
        	$header = ['Content-type:application/json', 'Accept-Charset:UTF-8'];
        	$options = [
        		CURLOPT_POST => true,
        		CURLOPT_HTTPHEADER => $header,
        		CURLOPT_POSTFIELDS => $parameters,
        		CURLOPT_RETURNTRANSFER => true,
        		CURLOPT_TIMEOUT => 60,
        		CURLOPT_SSL_VERIFYPEER => true,
        		CURLOPT_SSL_VERIFYHOST => 2
        	];
        	curl_setopt_array($curl, $options);
        	$tokens = json_decode(curl_exec($curl),true);
        	curl_close($curl);
	
		}
		$access_token=$tokens['access_token'];
		
		$url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
		$data=array('page'=>'pages/shop/shopClass','scene'=>$this->user['id'],'width'=>120);
		$res = httpRequest($url, $data, "json");
		//二维码图片地址	
		$qr_code ='assets/qcode/'.time().rand(1000, 9999).'.png';
		file_put_contents($qr_code ,$res);

        $qr = \think\Image::open($qr_code);
        $qr_code1 ='assets/qcode/'.time().rand(1000, 9999).'.png';
        $qr->thumb(60,60)->save($qr_code1);
        
        $image = \think\Image::open($bg);
        $img = 'assets/qcode/'.time().rand(1000, 9999).'.png';
        $image->water($qr_code1,[130,344])->save($img); 

        $image1 = \think\Image::open($img);
        $img1 = 'assets/qcode/'.time().rand(1000, 9999).'.png';
        $text1 = '你成功在余享寄存了';
        $text2 = '件闲置，你';
        $text3 = '的爱心行为不仅会帮助他人，也为节';
        $text4 = '能减排贡献出一份力量！';   
        $text5 = '余享闲置物品分享平台';   
        $text6 = date('Y年m月d日',time());   
        $text7 = '扫码查看分享内容';   
        $zt = ROOT_PATH.'public/assets/fonts/Alibaba-PuHuiTi-Regular.ttf';
        $image1->text($text1,$zt,11,'#000000',[50,200])->save($img1);

        $image2 = \think\Image::open($img1);
        $img2 = 'assets/qcode/'.time().rand(1000, 9999).'.png';
		if($share_num < 10){
			$w = 183;
			$w1 = 200;
		}elseif($share_num > 10 && $share_num < 100){
			$w = 176;
			$w1 = 200;
		}elseif($share_num > 100 && $share_num < 1000){
			$w = 176;
			$w1 = 203;
		}
        $image2->text($share_num,$zt,11,'#07C160',[$w,203])->save($img2);

        $image3 = \think\Image::open($img2);
        $img3 = 'assets/qcode/'.time().rand(1000, 9999).'.png';
        $image3->text($text2,$zt,11,'#000000',[$w1,200])->save($img3);

        $image4 = \think\Image::open($img3);
        $img4 = 'assets/qcode/'.time().rand(1000, 9999).'.png';
        $image4->text($text3,$zt,11,'#000000',[48,227])->save($img4);

        $image5 = \think\Image::open($img4);
        $img5 = 'assets/qcode/'.time().rand(1000, 9999).'.png';
        $image5->text($text4,$zt,11,'#000000',[48,254])->save($img5);

		//获取文字的宽度
		$fontBox = imagettfbbox(15, 0, $zt, $this->user['nickname']);
        $image6 = \think\Image::open($img5);
        $img6 = 'assets/qcode/'.time().rand(1000, 9999).'.png';
        $image6->text($this->user['nickname'],$zt,15,'#000000',[ceil((320 - $fontBox[2]) / 2),156])->save($img6);

        $image7 = \think\Image::open($img6);
        $img7 = 'assets/qcode/'.time().rand(1000, 9999).'.png';
        $image7->text($text5,$zt,11,'#2b2a2a',[135,295])->save($img7);

        $image8 = \think\Image::open($img7);
        $img8 = 'assets/qcode/'.time().rand(1000, 9999).'.png';
        $image8->text($text6,$zt,11,'#2b2a2a',[167,315])->save($img8);


        $image9 = \think\Image::open($img8);
        $img9 = 'assets/qcode/'.time().rand(1000, 9999).'.png';
        $image9->text($text7,$zt,9,'#2b2a2a',[113,410])->save($img9);

        unlink($qr_code);
        unlink($qr_code1);
        unlink($img);
        unlink($img1);
        unlink($img2);
        unlink($img3);
        unlink($img4);
        unlink($img5);
        unlink($img6);
        unlink($img7);
        unlink($img8);
		
		$this->success('操作成功','https://'.$_SERVER['HTTP_HOST'].'/'.$img9);
        
	}

	//获取朋友圈海报
	// public function product_ewm(){
	// 	$product_id = $this->request->request('product_id',0);
	// 	$wish_id = $this->request->request('wish_id',0);
	// 	if($product_id){
	// 		$bg="assets/pyq/pyq_hb.png";
	// 		$video_view =ProductModel::where(['id'=>$product_id])->value('video_img');
	// 		$data=array('page'=>'pages/shop/shopDetail','scene'=>$product_id,'width'=>120);
	// 	}else{
	// 		$bg="assets/pyq/pyq_hb_wish.png";
	// 		$video_view =ProductWish::where(['id'=>$wish_id])->value('video_img');
	// 		$data=array('page'=>'subone/shop/wishDetail','scene'=>$wish_id,'width'=>120);
	// 	}
	// 	$appid='wx84f56a63b9f63e03';
	//     $secret='98b698d6f873fdd2292684babd69c975';
	// 	$res= file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret);
	// 	$tokens=json_decode($res,true);
	// 	$access_token=$tokens['access_token'];
	// 	$url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
	// 	$res = httpRequest($url, $data, "json");
	// 	//二维码图片地址	
	// 	$qr_code ='assets/pyq/'.time().rand(1000, 9999).'.png';
	// 	file_put_contents($qr_code ,$res);

    //     $qr = \think\Image::open($qr_code);
    //     $qr_code1 ='assets/pyq/'.time().rand(1000, 9999).'.png';
    //     $qr->thumb(180,180)->save($qr_code1);
        
    //     $image = \think\Image::open($bg);
    //     $img = 'assets/pyq/'.time().rand(1000, 9999).'.png';
    //     $image->water($qr_code1,[80,850])->save($img); 


    //     $file = file_get_contents($video_view);
    //     $img_arr = explode('.', $video_view);
    //     $type = $img_arr[count($img_arr)-1];// 图片后缀
    //     // 写入保存图片，指定位位和文件名
    //     $video_view = 'assets/pyq/' . time().rand(1000, 9999) . '.' . $type;
    //     file_put_contents($video_view, $file);


    //     $qr = \think\Image::open($video_view);
    //     $product_img ='assets/pyq/'.time().rand(1000, 9999).'.png';
    //     $qr->thumb(520,520,2)->save($product_img);

    //     $image = \think\Image::open($img);
    //     $img1 = 'assets/pyq/'.time().rand(1000, 9999).'.png';
    //     $image->water($product_img,[80,285])->save($img1); 
	// 	unlink($qr_code);
    //     unlink($qr_code1);
    //     unlink($img);
    //     unlink($video_view);
    //     unlink($product_img);
	// 	$this->success('操作成功','https://'.$_SERVER['HTTP_HOST'].'/'.$img1);
	// }

	//获取朋友圈海报
	public function product_ewm(){
		$product_id = $this->request->request('product_id',0);
		$wish_id = $this->request->request('wish_id',0);
		if($product_id){
			$bg="assets/pyq/pyq_hb.png";
			$video_view =ProductModel::where(['id'=>$product_id])->value('video_img');
			$data=array('page'=>'pages/shop/shopDetail','scene'=>$product_id,'width'=>120);
			$text1 = '我在余享发现一件宝藏闲置';   
			$text2 = '好物，不花钱就能带走哦！'; 
		}else{
			$bg="assets/pyq/pyq_hb_wish.png";
			$video_view =ProductWish::where(['id'=>$wish_id])->value('video_img');
			$data=array('page'=>'subone/shop/wishDetail','scene'=>$wish_id,'width'=>120);
			$text1 = '我在余享发布了一个心愿，';   
			$text2 = '快来帮我实现吧！'; 
		}
		$appid='wx84f56a63b9f63e03';
	    $secret='98b698d6f873fdd2292684babd69c975';
		$res= file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret);
		$tokens=json_decode($res,true);
		if(empty($tokens['access_token'])){
		    $curl = curl_init("https://api.weixin.qq.com/cgi-bin/stable_token");
        	$parameters = json_encode(["grant_type"=>"client_credential", "appid"=>$appid,'secret'=>$secret]);
        	$header = ['Content-type:application/json', 'Accept-Charset:UTF-8'];
        	$options = [
        		CURLOPT_POST => true,
        		CURLOPT_HTTPHEADER => $header,
        		CURLOPT_POSTFIELDS => $parameters,
        		CURLOPT_RETURNTRANSFER => true,
        		CURLOPT_TIMEOUT => 60,
        		CURLOPT_SSL_VERIFYPEER => true,
        		CURLOPT_SSL_VERIFYHOST => 2
        	];
        	curl_setopt_array($curl, $options);
        	$tokens = json_decode(curl_exec($curl),true);
        	curl_close($curl);
	
		}
		$access_token=$tokens['access_token'];
		$url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
		$res = httpRequest($url, $data, "json");
		//二维码图片地址	
		$qr_code ='assets/pyq/'.time().rand(1000, 9999).'.png';
		file_put_contents($qr_code ,$res);

        $qr = \think\Image::open($qr_code);
        $qr_code1 ='assets/pyq/'.time().rand(1000, 9999).'.png';
        $qr->thumb(180,180)->save($qr_code1);
        
        $image = \think\Image::open($bg);
        $img = 'assets/pyq/'.time().rand(1000, 9999).'.png';
        $image->water($qr_code1,[80,850])->save($img); 


        $file = file_get_contents($video_view);
        $img_arr = explode('.', $video_view);
        $type = $img_arr[count($img_arr)-1];// 图片后缀
        // 写入保存图片，指定位位和文件名
        $video_view = 'assets/pyq/' . time().rand(1000, 9999) . '.' . $type;
        file_put_contents($video_view, $file);

		//获取用户头像
		if(!empty($this->user['id'])){
			$avatar = User::where(['id'=>$this->user['id']])->value('avatar');
		}else{
			$avatar = 'https://app.yuxiang123.cn/Uploads/2022-07-14/62cf80939dbca.png';
		}
		$file = file_get_contents($avatar);
        $img_arr = explode('.', $avatar);
        $type = $img_arr[count($img_arr)-1];// 图片后缀
        // 写入保存图片，指定位位和文件名
        $avatar = 'assets/pyq/' . time().rand(1000, 9999) . '.' . $type;
        file_put_contents($avatar, $file);

		$avatar_image = \think\Image::open($avatar);
		$avatar_img ='assets/pyq/'.time().rand(1000, 9999).'.png';
		$avatar_image->thumb(100,100,2)->save($avatar_img);
		$avatar_img1 ='assets/pyq/'.time().rand(1000, 9999).'.png';
		$this->test($avatar_img,$avatar_img1);
		
		$image = \think\Image::open($img);
        $img2 = 'assets/pyq/'.time().rand(1000, 9999).'.png';
        $image->water($avatar_img1,[80,300])->save($img2); 

        $qr = \think\Image::open($video_view);
        $product_img ='assets/pyq/'.time().rand(1000, 9999).'.png';
        $qr->thumb(520,368,3)->save($product_img);

        $image = \think\Image::open($img2);
        $img1 = 'assets/pyq/'.time().rand(1000, 9999).'.png';
        $image->water($product_img,[80,437])->save($img1); 
        
        $zt = ROOT_PATH.'public/assets/fonts/Alibaba-PuHuiTi-Regular.ttf';
        $image1 = \think\Image::open($img1);
        $img4 = 'assets/pyq/'.time().rand(1000, 9999).'.png';
        $image1->text($text1,$zt,24,'#000000',[200,320])->save($img4);
        
        $image2 = \think\Image::open($img4);
        $img5 = 'assets/pyq/'.time().rand(1000, 9999).'.png';
        $image2->text($text2,$zt,24,'#000000',[200,360])->save($img5);
        
		unlink($qr_code);
        unlink($qr_code1);
        unlink($avatar);
        unlink($avatar_img);
        unlink($avatar_img1);
        unlink($img);
        unlink($img1);
        unlink($img2);
        unlink($img4);
        unlink($video_view);
        unlink($product_img);
		$this->success('操作成功','https://'.$_SERVER['HTTP_HOST'].'/'.$img5);
	}

	/**
	* $url:是头像路径，$path:是保存路径（../是根目录）
	**/
	public function resize_img($url,$path='../'){ //先缩略
		$imgname = $path.uniqid().'.jpg';
		$file = $url;
		list($width, $height) = getimagesize($file); //获取原图尺寸
		$percent = (140/$width);
		//缩放尺寸
		$newwidth = $width * $percent;
		$newheight = $height * $percent;
		$src_im = imagecreatefromjpeg($file);
		$dst_im = imagecreatetruecolor($newwidth, $newheight);
		imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		imagejpeg($dst_im, $imgname); //输出压缩后的图片
		imagedestroy($dst_im);
		imagedestroy($src_im);
		return $imgname;
	}

	public function test($url,$dest_path='../'){  //处理圆形
		$w = 100;  $h=100; // original size  
		$original_path= $url;  
		$src = imagecreatefromstring(file_get_contents($original_path));  
		$newpic = imagecreatetruecolor($w,$h);  
		imagealphablending($newpic,false);  
		$transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127);  
		$r=$w/2;  
		for($x=0;$x<$w;$x++)  
			for($y=0;$y<$h;$y++){  
				$c = imagecolorat($src,$x,$y);  
				$_x = $x - $w/2;  
				$_y = $y - $h/2;  
				if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){  
					imagesetpixel($newpic,$x,$y,$c);  
				}else{  
					imagesetpixel($newpic,$x,$y,$transparent);  
				}  
			}  
		imagesavealpha($newpic, true);  
		imagepng($newpic, $dest_path);  
		imagedestroy($newpic);  
		imagedestroy($src);  
		// unlink($url);  
		return $dest_path;  
	}  

	//新增收购商品
	public function product_take(){
		$weight = $this->request->request('weight',0);  //重量
		$num = $this->request->request('num',0);  //件数
		$address = $this->request->request('address','');  //详细地址
		$province = $this->request->request('province','');  //省
		$city = $this->request->request('address','');  //市
		$area = $this->request->request('area','');  //区
		$name = $this->request->request('name','');  //昵称
		$phone = $this->request->request('phone','');  //手机号
		$imgs = $this->request->request('imgs','');  //图片
		$type = $this->request->request('type_status','');  //物品类型

		$arr = [
			'user_id'=>$this->user['id'],
			'type'=>$type,
			'address_username'=>$name,
			'address_tel'=>$phone,
			'address_province'=>$province,
			'address_city'=>$city,
			'address_district'=>$area,
			'address_detail'=>$address,
			'imgs'=>$imgs,
			'num'=>$num,
			'weight'=>$weight,
			'add_time'=>time(),
		];
		$res = ProductTake::insert($arr);
		if($res){
			$this->success('操作成功');
		}else{
			$this->error('操作失败');
		}



	}

	//订单合并购物车
	public function merge_cart(){
		$weight = $this->request->request('weight',0);  //重量
		$olb_yunfei = $this->request->request('yunfei',0);  //老的运费
		$freight = $this->request->request('freight',0);  //支付总金额
		$price = $this->request->request('price',0);  //商品总金额
		$address_province = $this->request->request('address_province','');  //城市
		$address_city = $this->request->request('address_city','');  //城市
		$address_district = $this->request->request('address_district','');  //城市
		$address_detail = $this->request->request('address_detail','');  //城市
		$status = $this->request->request('status',0);   //状态  1加  -1减
		$new_product_id = $this->request->request('new_product_id',0);

		$product = ProductModel::where(['id'=>$new_product_id])->find();


		$order = [];
        $order['id'] = -1;
        $order['address_city'] = $address_city;
        $order['product_id'] = $new_product_id;
        $order['address'] = $address_province.$address_city.$address_district.$address_detail;

		$new_yunfei =  get_delivery_costs($order,$weight); 

		//$new_yunfei = get_price(['weight'=>$weight.'kg','city'=>$product['city']],$address_city);  //新的运费



		if($status == 1){
			$price = bcadd($price, $product['price'], 2);
			//新的总金额计算 = 新的运费 - 老的运费 + 支付总金额
			$freight = bcadd(bcsub($new_yunfei, $olb_yunfei, 2) , $freight,2);
			if($product['type_status'] == 3){  //商场成交物品
				$freight = bcadd($freight, $product['price'], 2);
			}
		}else{
			$price = bcsub($price, $product['price'], 2);
			//新的总金额计算 = 支付总金额 - (老的运费- 新的运费) 
			$freight = bcsub($freight,bcsub($olb_yunfei, $new_yunfei, 2) ,2);
			if($product['type_status'] == 3){  //商场成交物品
				$freight = bcsub($freight, $product['price'], 2);
			}
		}
		$this->success('操作成功',['info'=>$product,'yunfei'=>$new_yunfei,'freight'=>$freight,'price'=>$price]);
	}


	
	//获取爱心榜单
	public function getLoveList(){
		$page = $this->request->request('page',1);
		$pagesize = $this->request->request('pagesize',10);

		$where = [
			'status'=>1,
			'type_status'=>1
		];

		$list = ProductModel::field('count(user_id) as ids,user_id')->where($where)->limit(($page-1)*$pagesize,$pagesize)->order('ids desc')->group('user_id')->select();

		foreach ($list as $k=>&$v) {
			$user = User::where(['id'=>$v['user_id']])->find();
			$v['nickname'] = $user['nickname'];
			$v['avatar'] = $user['avatar'];

			$res= explode('oa.yuxiang123.cn/',$v['avatar']);
			if ((!empty($res[1]) &&  !file_exists($res[1])) || !$v['nickname']) {
				unset($list[$k]);
			} 
		}
		$list = array_values($list);
		$this->success('操作成功',$list);
	}



}